<?php
App::uses('AppModel', 'Model');
/**
 * Domain Model
 *
 * @property Server $Server
 * @property Serversip $Serversip
 * @property Article $Article
 * @property Comment $Comment
 * @property Keyword $Keyword
 * @property Post $Post
 * @property Statistic $Statistic
 * @property Menus $Menus
 * @property Project $Project
 * @property User $User
 */
class Domain extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'name';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'server_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'serversip_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Server' => array(
			'className' => 'Server',
			'foreignKey' => 'server_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Serversip' => array(
			'className' => 'Serversip',
			'foreignKey' => 'serversip_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Article' => array(
			'className' => 'Article',
			'foreignKey' => 'domain_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Configuration' => array(
				'className' => 'Configuration',
				'foreignKey' => 'domain_id',
				'dependent' => true,
				'conditions' => '',
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'exclusive' => '',
				'finderQuery' => '',
				'counterQuery' => ''
		),
		'Comment' => array(
			'className' => 'Comment',
			'foreignKey' => 'domain_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),		
		'Post' => array(
			'className' => 'Post',
			'foreignKey' => 'domain_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Statistic' => array(
			'className' => 'Statistic',
			'foreignKey' => 'domain_id',
			'dependent' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'Submenu' => array(
			'className' => 'Submenu',
			'foreignKey' => 'domain_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'Menus' => array(
			'className' => 'Menus',
			'joinTable' => 'domains_menuses',
			'foreignKey' => 'domain_id',
			'associationForeignKey' => 'menus_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => array('Menus.position','Menus.lft'),
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'Project' => array(
			'className' => 'Project',
			'joinTable' => 'domains_projects',
			'foreignKey' => 'domain_id',
			'associationForeignKey' => 'project_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'User' => array(
			'className' => 'User',
			'joinTable' => 'domains_users',
			'foreignKey' => 'domain_id',
			'associationForeignKey' => 'user_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		),
		'Authorization' => array(
			'className' => 'Authorization',
			'joinTable' => 'domains_authorizations',
			'foreignKey' => 'domain_id',
			'associationForeignKey' => 'authorization_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'Link' => array(
			'className' => 'Link',
			'joinTable' => 'domains_links',
			'foreignKey' => 'domain_id',
			'associationForeignKey' => 'link_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => '',
			'dependent' => true
		),
		'Upload' => array(
				'className' => 'Upload',
				'joinTable' => 'domains_uploads',
				'foreignKey' => 'domain_id',
				'associationForeignKey' => 'upload_id',
				'unique' => 'keepExisting',
				'conditions' => '',
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'finderQuery' => '',
				'deleteQuery' => '',
				'insertQuery' => '',
				'dependent' => true
		),
		'Job' => array(
				'className' => 'Job',
				'joinTable' => 'domains_jobs',
				'foreignKey' => 'domain_id',
				'associationForeignKey' => 'job_id',
				'unique' => 'keepExisting',
				'conditions' => '',
				'fields' => '',
				'order' => '',
				'limit' => '',
				'offset' => '',
				'finderQuery' => '',
				'deleteQuery' => '',
				'insertQuery' => '',
				'dependent' => true
		),
	);
	
	
	public function filter($f=null){
		Switch($f){
			case "articles":
				$this->hasMany = array(
						'Article' => array(
							'className' => 'Article',
							'foreignKey' => 'domain_id',
							'dependent' => false,
							'conditions' => '',
							'fields' => '',
							'order' => '',
							'limit' => '',
							'offset' => '',
							'exclusive' => '',
							'finderQuery' => '',
							'counterQuery' => ''
				));
				$this->hasAndBelongsToMany = array();
			break;
			case "posts":
				$this->hasMany = array(
					'Post' => array(
						'className' => 'Post',
						'foreignKey' => 'domain_id',
						'dependent' => false,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
					));
				$this->hasAndBelongsToMany = array();
			break;
			case "comments":
				$this->hasMany = array(
					'Comment' => array(
						'className' => 'Comment',
						'foreignKey' => 'domain_id',
						'dependent' => false,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
					));
				$this->hasAndBelongsToMany = array();
			break;
			case "keywords":
				$this->hasMany = array(
					'Keyword' => array(
						'className' => 'Keyword',
						'foreignKey' => 'domain_id',
						'dependent' => false,
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
					));
				$this->hasAndBelongsToMany = array();
			break;
			case "settings":
				$this->hasMany = array(
					'Configuration' => array(
					'className' => 'Configuration',
					'foreignKey' => 'domain_id',
					'dependent' => false,
					'conditions' => '',
					'fields' => '',
					'order' => 'Configuration.cfg_group_id',
					'limit' => '',
					'offset' => '',
					'exclusive' => '',
					'finderQuery' => '',
					'counterQuery' => ''
				));
				$this->hasAndBelongsToMany = array();
			break;
			default:
				$this->hasMany = array();
			break;
		}		
	}

// 	public function beforeSave( $options = array() ) {
// 		$this->data['Domain']['cache'] = 1;
// 		return true;
// 	}
}
